package com.huluhome.controller;

import com.huluhome.constant.Configuration;
import com.huluhome.constant.DictEnum;
import com.huluhome.domain.Article;
import com.huluhome.domain.ArticleQueryModel;
import com.huluhome.domain.ArticleReply;
import com.huluhome.domain.Category;
import com.huluhome.domain.HotWord;
import com.huluhome.service.IArticleService;
import com.huluhome.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Callback;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;

/**
 * Created by k on 2017/9/23.
 */

@Controller
public class ArticleController {
    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IArticleService articleService;




    /**
     * 根据条件获取article列表
     * @param request
     * @param categoryId
     * @param pageIndex
     * @return
     */
    private String getArticles(HttpServletRequest request, int categoryId,int pageIndex) {
        ArticleQueryModel articleQueryModel = new ArticleQueryModel();
        articleQueryModel.setStatus(DictEnum.STATUS_ENBALE.getIntCodeValue());
        articleQueryModel.setRecommId(DictEnum.SSTATUS_ALL.getIntCodeValue());
        articleQueryModel.setRowNumber(pageIndex);
        articleQueryModel.setPageSize(Integer.parseInt(Configuration.pageSize));
        articleQueryModel.setCategoryId(categoryId);
        List<Article> articleList= articleService.getArticles(articleQueryModel);
        request.setAttribute("articleList", articleList);

        articleQueryModel.setRecommId(DictEnum.HOME_ARTICLE_REOMMEND.getIntCodeValue());
        List<Article> homeArticleList= articleService.getArticles(articleQueryModel);
        request.setAttribute("homeArticleList", homeArticleList);


        int prevPageIndex = pageIndex> 0 ?pageIndex-1:pageIndex;
        int nextPageIndex = pageIndex+1;
        if (categoryId > 0) {
            request.setAttribute("prevPageIndexUrl", "/list_category_"+ prevPageIndex);
            if (articleList.size() == 0){
                request.setAttribute("nextPageIndexUrl", "/list_category_"+ prevPageIndex);
            }else{
                request.setAttribute("nextPageIndexUrl", "/list_category_"+ nextPageIndex);
            }

        }else{
            request.setAttribute("prevPageIndexUrl", "/"+ prevPageIndex);
            if (articleList.size() == 0) {
                request.setAttribute("nextPageIndexUrl", "/"+ prevPageIndex);
            } else {
                request.setAttribute("nextPageIndexUrl", "/"+ nextPageIndex);
            }

        }

        List<Category> categories = categoryService.getCategories(DictEnum.STATUS_ENBALE.getIntCodeValue());
        request.setAttribute("categories", categories);

        return "index";
    }

    /**
     * 获取默认文章列表
     * @param request
     * @return
     */
    @RequestMapping("/")
    public Callable<String> index(HttpServletRequest request) {
        return new Callable<String>() {
            @Override
            public String call() throws Exception {
                Thread.sleep(2000);
                return getArticles(request,0,0);
            }
        };
//       return getArticles(request,0,0);
    }

    /**
     * 获取默认文字列表的分页
     * @param request
     * @param pageIndex
     * @return
     */
    @RequestMapping("/{pageIndex}")
    public String index(HttpServletRequest request,@PathVariable int pageIndex) {
        return getArticles(request,0,pageIndex);
    }


    /**
     * 获取类别文章列表
     * @param request
     * @param categoryId
     * @return
     */
    @RequestMapping(value = "/list_category_{categoryId}")
    public String getArticleList(HttpServletRequest request,@PathVariable int categoryId) {
        return getArticles(request,categoryId,0);
    }

    /**
     * 获取类别文字列表的分页
     * @param request
     * @param categoryId
     * @param pageIndex
     * @return
     */
    @RequestMapping(value = "/list_category_{categoryId}_{pageIndex}")
    public String getArticleList(HttpServletRequest request,@PathVariable int categoryId, @PathVariable int pageIndex) {
        return getArticles(request,categoryId,pageIndex);
    }



    @RequestMapping(value = "/details/{articleId}")
    public String details(HttpServletRequest request, @PathVariable int articleId) {
        ArticleQueryModel articleQueryModel = new ArticleQueryModel();
        articleQueryModel.setStatus(DictEnum.STATUS_ENBALE.getIntCodeValue());
        articleQueryModel.setRecommId(DictEnum.COMMON_ARTICLE_REOMMEND.getIntCodeValue());
        articleQueryModel.setId(articleId);
        articleQueryModel.setRecommId(DictEnum.SSTATUS_ALL.getIntCodeValue());
        Article article = articleService.getArticle(articleQueryModel);
        request.setAttribute("article",article);

        List<Category> categories = categoryService.getCategories(DictEnum.STATUS_ENBALE.getIntCodeValue());
        request.setAttribute("categories", categories);
        return "details";
    }

    @RequestMapping("/add_reply")
    @ResponseBody
    public ArticleReply addReply(int articleId, String content) {
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setUserAccount("");
        articleReply.setCreateTime(new Date());
        articleReply.setStatus(1);
        articleReply.setContent(content);
        articleService.addArticleReply(articleReply);
        return articleReply;
    }

    @RequestMapping("/replies")
    @ResponseBody
    public List<ArticleReply> getArticleReplyList(int articleId) {
        if (articleId == 0){
            List<ArticleReply> list = articleService.getArticleReplies(null);
            return list;
        }
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        return articleService.getArticleReplies(articleReply);
    }

    @RequestMapping(value = "/list/{recommId}")
    @ResponseBody
    public List<Article> getRecomendArticle(@PathVariable int recommId) {
        ArticleQueryModel articleQueryModel = new ArticleQueryModel();
        articleQueryModel.setRecommId(recommId);
        articleQueryModel.setStatus(DictEnum.STATUS_ENBALE.getIntCodeValue());
        List<Article> articleList = articleService.getArticles(articleQueryModel);
        return articleList;
    }

    @RequestMapping("/hotwords")
    @ResponseBody
    public List<HotWord> getHotWords() {
        return articleService.getHotWords();
    }

    @RequestMapping("/hello")
    public String hello(HttpServletRequest request) {
        List<Category> categories = categoryService.getCategories(-1);
        ArticleQueryModel articleQueryModel = new ArticleQueryModel();
        articleQueryModel.setCategoryId(1);
        articleQueryModel.setStatus(1);
        List<Article> articleList= articleService.getArticles(articleQueryModel);
        request.setAttribute("categories", articleList);
        return "hello";
    }

    @RequestMapping("/test")
    @ResponseBody
    public Callable<HotWord> test() {
        HotWord hotWord = new HotWord();
        hotWord.setArticleId(1);
        hotWord.setKeyword("testkeyword");
        return new Callable<HotWord>() {
            @Override
            public HotWord call() throws Exception {
                return hotWord;
            }
        };
    }

}
